﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.DynamicDataDisplay.Charts;
using Microsoft.Research.DynamicDataDisplay;

namespace VerticalRangeSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// In this sample Mouse zooming and Keybaord navigations are removed
    /// Vertical range is added in mouse move.
    /// </summary>
    public partial class MainWindow : Window
    {
        VerticalRange vRange = new VerticalRange();
        Segment segment = new Segment();
        bool isMouseDown = false;
        double psX = 0.0d;
        public MainWindow()
        {
            InitializeComponent();
            this.plotter.MouseDown += new MouseButtonEventHandler(plotter_MouseDown);
            this.plotter.MouseUp += new MouseButtonEventHandler(plotter_MouseUp);
            this.plotter.MouseMove += new MouseEventHandler(plotter_MouseMove);
            this.plotter.Children.Remove(this.plotter.MouseNavigation);
            this.plotter.Children.Remove(this.plotter.KeyboardNavigation);
            this.plotter.Children.Add(this.vRange);
            this.segment.StartPoint = new Point(0.3, 0.45);
            this.segment.EndPoint = new Point(0.5, 0.6);
            this.plotter.Children.Add(this.segment);
        }

        void plotter_MouseMove(object sender, MouseEventArgs e)
        {
            if (!this.isMouseDown)
            {
                return;
            }

            Point pt = e.GetPosition(this.plotter);
            Point ps = this.plotter.Viewport.Transform.ScreenToViewport(pt);
            if (ps.X > this.vRange.Value1)
            {
                this.vRange.Value2 = ps.X;
            }
            else
            {
                this.vRange.Value2 = this.psX;
                this.vRange.Value1 = ps.X;
            }
        }

        void plotter_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.isMouseDown = false;
            this.vRange.Visibility = Visibility.Collapsed;
        }

        void plotter_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.isMouseDown = true;
            this.vRange.Visibility = Visibility.Visible;
            Point pt = e.GetPosition(this.plotter);
            Point ps = this.plotter.Viewport.Transform.ScreenToViewport(pt);
            this.vRange.Value1 = ps.X;
            this.psX = ps.X;
            this.vRange.Value2 = ps.X + (ps.X * 10) / 100;
        }
    }
}
